﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tunnel.Tools.FcmsSupport;

namespace Tunnel.DevicesControls
{
    public partial class FCMSForm : Form
    {

        // 当前选择的情报板设备ID
        string fcmsId;

        string fcmsPlaylistID;

        // 当前选择的播放列表ID
        string playListId;

        public string selectFcmsParamName;

        string controlSwitch;

        DataTable dtPlayListInfo;
        DisplayElement element = new DisplayElement();
        private Item JX_msg = new Item();
        int simFontSize;
        private int scrCoeff = 2;
        private string simFont = "宋体";
        public FCMSForm()
        {
            InitializeComponent();
            getFcmss();

            getPlayList();

            fillTheGridView();

            cbFcmss_SelectedValueChanged(null, null);
        }

        public FCMSForm(string deviceId)
        {
            InitializeComponent();
            getFcmss();

            getPlayList();

            fillTheGridView();

            cbFcmss_SelectedValueChanged(null, null);
            foreach (DataRowView item in cbFcmss.Items)
            {
                if (item["IDandPARAM"].ToString().Contains(deviceId))
                {
                    cbFcmss.SelectedValue = item["IDandPARAM"].ToString();
                }
            }
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            using (SolidBrush b = new SolidBrush(dgvPlayList.RowHeadersDefaultCellStyle.ForeColor))
            {
                e.Graphics.DrawString("第" + (e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture) + "屏", e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
            }

        }

        private int FullCharCount(string text)
        {
            if (text != null)
            {
                int num = Encoding.Default.GetByteCount(text);
                int len = text.Length;
                return num - len;
            }
            return 0;
        }
        private int HalfCharCount(string text)
        {
            if (text != null)
            {
                int num = Encoding.Default.GetByteCount(text);
                int len = text.Length;
                return len - (num - len);
            }
            return 0;
        }
        /// <summary>
        /// 得到一个屏幕内可以容纳的半角字符总数
        /// </summary>
        /// <param name="fontSize">字体大小</param>
        /// <param name="screenWidth">屏幕宽度</param>
        /// <param name="screenHeight">屏幕高度</param>
        /// <returns>可容纳的半角字符总数</returns>
        private int GetMaxSbcTextCountInScreen(int fontSize, int screenWidth, int screenHeight)
        {
            int maxInOneLine = 2 * (screenWidth / fontSize);
            int maxLines = screenHeight / fontSize;
            return maxInOneLine * maxLines;
        }

        private void AutoSplitScreen(int dataGridViewRowIndex)
        {
            if (dataGridViewRowIndex > -1 && dgvPlayList.Rows[dataGridViewRowIndex].Cells["字号"].Value != null && dgvPlayList.Rows[dataGridViewRowIndex].Cells["显示内容"].Value != null && dgvPlayList.Rows[dataGridViewRowIndex].Cells["字号"].Value != null && dgvPlayList.Rows[dataGridViewRowIndex].Cells["字体颜色"].Value != null)
            {
                DataGridViewTextBoxCell textboxCell = (DataGridViewTextBoxCell)dgvPlayList.Rows[dataGridViewRowIndex].Cells["显示内容"];
                DataGridViewComboBoxCell comboxCell = (DataGridViewComboBoxCell)dgvPlayList.Rows[dataGridViewRowIndex].Cells["字号"];
                int max = GetMaxSbcTextCountInScreen(Convert.ToInt32(comboxCell.Value.ToString()), 128, 64);
                string noEnterString = textboxCell.Value?.ToString().Replace(@"\n", string.Empty);
                if (textboxCell.Value != null && Encoding.Default.GetByteCount(noEnterString) > max)
                {
                    string lastChar = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max).Substring(Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max).Length - 1, 1);
                    if (lastChar != "?") //判断半角字符数量是否为偶数个
                    {
                        string text1 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max);
                        string text2 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), max, Encoding.Default.GetByteCount(noEnterString) - max);
                        dgvPlayList.CellValueChanged -= dgvPlayList_CellValueChanged;
                        //textboxCell.Value = insertEnterIntoString(text1, comboxCell.Value.ToString());//将本屏幕内剩余的字符串添加回车符
                        textboxCell.Value = InsertEnterIntoString(text1, Convert.ToInt32(comboxCell.Value.ToString()), 128, 64);
                        dgvPlayList.Rows.Insert(dataGridViewRowIndex + 1, new object[] { text2, dgvPlayList.Rows[dataGridViewRowIndex].Cells["停留时间"].Value.ToString(), dgvPlayList.Rows[dataGridViewRowIndex].Cells["字号"].Value.ToString(), dgvPlayList.Rows[dataGridViewRowIndex].Cells["字体颜色"].Value.ToString(), dgvPlayList.Rows[dataGridViewRowIndex].Cells["坐标X"].Value.ToString(), dgvPlayList.Rows[dataGridViewRowIndex].Cells["坐标Y"].Value.ToString() });
                        dgvPlayList.CellValueChanged += dgvPlayList_CellValueChanged;

                    }
                    else //如果是奇数个，则需要往前提前半个全角字符截取
                    {
                        string text1 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max - 1);
                        string text2 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), max - 1, Encoding.Default.GetByteCount(noEnterString) - max + 1);
                        dgvPlayList.CellValueChanged -= dgvPlayList_CellValueChanged;
                        //textboxCell.Value = insertEnterIntoString(text1, comboxCell.Value.ToString());//将本屏幕内剩余的字符串添加回车符
                        textboxCell.Value = InsertEnterIntoString(text1, Convert.ToInt32(comboxCell.Value.ToString()), 128, 64);
                        dgvPlayList.Rows.Insert(dataGridViewRowIndex + 1, new object[] { text2, dgvPlayList.Rows[dataGridViewRowIndex].Cells["停留时间"].Value.ToString(), dgvPlayList.Rows[dataGridViewRowIndex].Cells["字号"].Value.ToString(), dgvPlayList.Rows[dataGridViewRowIndex].Cells["字体颜色"].Value.ToString(), dgvPlayList.Rows[dataGridViewRowIndex].Cells["坐标X"].Value.ToString(), dgvPlayList.Rows[dataGridViewRowIndex].Cells["坐标Y"].Value.ToString() });
                        dgvPlayList.CellValueChanged += dgvPlayList_CellValueChanged;
                    }
                }
                if (Encoding.Default.GetByteCount(noEnterString) <= max)
                {
                    //textboxCell.Value = insertEnterIntoString(noEnterString, comboxCell.Value.ToString());
                    textboxCell.Value = InsertEnterIntoString(noEnterString, Convert.ToInt32(comboxCell.Value.ToString()), 128, 64);
                }
                DataGridViewTextBoxCell textboxCellX = (DataGridViewTextBoxCell)dgvPlayList.Rows[dataGridViewRowIndex].Cells["坐标X"];
                DataGridViewTextBoxCell textboxCellY = (DataGridViewTextBoxCell)dgvPlayList.Rows[dataGridViewRowIndex].Cells["坐标Y"];
                int fontsize = int.Parse(comboxCell.Value.ToString());
                RefreshFCMS();//将新的字符串刷入预览LABEL，之后按照新的字符串计算位置
                textboxCellX.Value = (128 - ((fontsize / 2) * MaxLineLength(textboxCell.Value.ToString()))) / 2;
                textboxCellY.Value = (64 - (fontsize * GetLinesCount(textboxCell.Value.ToString()))) / 2;
                this.lableTextPrewiew.Location = new Point((int)((this.panelUp.Width - this.lableTextPrewiew.Width) / 2), (int)((this.panelUp.Height - this.lableTextPrewiew.Height) / 2));
                RefreshFCMS();
            }
        }

        private void dgvPlayList_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {

            AutoSplitScreen(e.RowIndex);
            //if (e.RowIndex > -1&& dgvPlayList.Rows[e.RowIndex].Cells["字号"].Value!=null&& dgvPlayList.Rows[e.RowIndex].Cells["显示内容"].Value != null&& dgvPlayList.Rows[e.RowIndex].Cells["字号"].Value != null&& dgvPlayList.Rows[e.RowIndex].Cells["字体颜色"].Value != null)
            //{
            //    DataGridViewTextBoxCell textboxCell = (DataGridViewTextBoxCell)dgvPlayList.Rows[e.RowIndex].Cells["显示内容"];
            //    DataGridViewComboBoxCell comboxCell = (DataGridViewComboBoxCell)dgvPlayList.Rows[e.RowIndex].Cells["字号"];

            //    int max = GetMaxSbcTextCountInScreen(Convert.ToInt32(comboxCell.Value.ToString()),128,64);

            //    string noEnterString = textboxCell.Value?.ToString().Replace(@"\n", string.Empty);

            //    if (textboxCell.Value != null && Encoding.Default.GetByteCount(noEnterString) > max)
            //    {
            //        string lastChar = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max).Substring(Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max).Length - 1, 1);
            //        if (lastChar != "?") //判断半角字符数量是否为偶数个
            //        {
            //            string text1 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max);
            //            string text2 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), max, Encoding.Default.GetByteCount(noEnterString) - max);
            //            dgvPlayList.CellValueChanged -= dgvPlayList_CellValueChanged;
            //            //textboxCell.Value = insertEnterIntoString(text1, comboxCell.Value.ToString());//将本屏幕内剩余的字符串添加回车符
            //            textboxCell.Value = insertEnterIntoString2(text1, Convert.ToInt32(comboxCell.Value.ToString()), 128, 64);
            //            dgvPlayList.Rows.Insert(e.RowIndex + 1, new object[] { text2, dgvPlayList.Rows[e.RowIndex].Cells["停留时间"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["字号"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["字体颜色"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["坐标X"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["坐标Y"].Value.ToString() });
            //            dgvPlayList.CellValueChanged += dgvPlayList_CellValueChanged;

            //        }
            //        else //如果是奇数个，则需要往前提前半个全角字符截取
            //        {
            //            string text1 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max - 1);
            //            string text2 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), max - 1, Encoding.Default.GetByteCount(noEnterString) - max + 1);
            //            dgvPlayList.CellValueChanged -= dgvPlayList_CellValueChanged;
            //            //textboxCell.Value = insertEnterIntoString(text1, comboxCell.Value.ToString());//将本屏幕内剩余的字符串添加回车符
            //            textboxCell.Value = insertEnterIntoString2(text1, Convert.ToInt32(comboxCell.Value.ToString()), 128, 64);
            //            dgvPlayList.Rows.Insert(e.RowIndex + 1, new object[] { text2, dgvPlayList.Rows[e.RowIndex].Cells["停留时间"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["字号"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["字体颜色"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["坐标X"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["坐标Y"].Value.ToString() });
            //            dgvPlayList.CellValueChanged += dgvPlayList_CellValueChanged;
            //        }
            //    }
            //    if (Encoding.Default.GetByteCount(noEnterString) <=max)
            //    {
            //        //textboxCell.Value = insertEnterIntoString(noEnterString, comboxCell.Value.ToString());
            //        textboxCell.Value = insertEnterIntoString2(noEnterString, Convert.ToInt32(comboxCell.Value.ToString()), 128, 64);
            //    }
            //    DataGridViewTextBoxCell textboxCellX = (DataGridViewTextBoxCell)dgvPlayList.Rows[e.RowIndex].Cells["坐标X"];
            //    DataGridViewTextBoxCell textboxCellY = (DataGridViewTextBoxCell)dgvPlayList.Rows[e.RowIndex].Cells["坐标Y"];
            //    int fontsize = int.Parse(comboxCell.Value.ToString());
            //    refreshFCMS();//将新的字符串刷入预览LABEL，之后按照新的字符串计算位置
            //    textboxCellX.Value = (128 - ((fontsize / 2) * MaxLineLength(textboxCell.Value.ToString()))) / 2;
            //    textboxCellY.Value = (64 - (fontsize * GetLinesCount(textboxCell.Value.ToString()))) / 2;
            //    this.lableTextPrewiew.Location = new Point((int)((this.panelUp.Width - this.lableTextPrewiew.Width) / 2), (int)((this.panelUp.Height - this.lableTextPrewiew.Height) / 2));
            //    refreshFCMS();
            //    #region 无用代码
            //    //if (comboxCell.Value.ToString() == "32")
            //    //{
            //    //    //int max = 16;
            //    //    if (textboxCell.Value != null && Encoding.Default.GetByteCount(textboxCell.Value?.ToString()) > max)
            //    //    {                        
            //    //        if (HalfCharCount(textboxCell.Value.ToString()) % 2 == 0) //判断半角字符数量是否为偶数个
            //    //        {
            //    //            string text1 = Encoding.Default.GetString(Encoding.Default.GetBytes(textboxCell.Value?.ToString()), 0, max);
            //    //            string text2 = Encoding.Default.GetString(Encoding.Default.GetBytes(textboxCell.Value?.ToString()), max, Encoding.Default.GetByteCount(textboxCell.Value?.ToString()) - max);
            //    //            textboxCell.Value = text1;
            //    //            dataGridView1.Rows.Insert(e.RowIndex + 1, new object[] { text2, dataGridView1.Rows[e.RowIndex].Cells["停留时间"].Value.ToString(), dataGridView1.Rows[e.RowIndex].Cells["字号"].Value.ToString(), dataGridView1.Rows[e.RowIndex].Cells["字体颜色"].Value.ToString() });

            //    //        }
            //    //        else //如果是奇数个，则需要往前提前半个全角字符截取
            //    //        {
            //    //            string text1 = Encoding.Default.GetString(Encoding.Default.GetBytes(textboxCell.Value?.ToString()), 0, max - 1);
            //    //            string text2 = Encoding.Default.GetString(Encoding.Default.GetBytes(textboxCell.Value?.ToString()), max - 1, Encoding.Default.GetByteCount(textboxCell.Value?.ToString()) - max+1);
            //    //            textboxCell.Value = text1;
            //    //            dataGridView1.Rows.Insert(e.RowIndex + 1, new object[] { text2, dataGridView1.Rows[e.RowIndex].Cells["停留时间"].Value.ToString(), dataGridView1.Rows[e.RowIndex].Cells["字号"].Value.ToString(), dataGridView1.Rows[e.RowIndex].Cells["字体颜色"].Value.ToString() });
            //    //        }
            //    //    }


            //    //}
            //    //if (comboxCell.Value.ToString() == "16")
            //    //{
            //    //    //int max = 36;
            //    //    if (textboxCell.Value != null && Encoding.Default.GetByteCount(textboxCell.Value?.ToString()) > max)
            //    //    {
            //    //        if (HalfCharCount(textboxCell.Value.ToString()) % 2 == 0)
            //    //        {
            //    //            string text1 = Encoding.Default.GetString(Encoding.Default.GetBytes(textboxCell.Value?.ToString()), 0, max);
            //    //            string text2 = Encoding.Default.GetString(Encoding.Default.GetBytes(textboxCell.Value?.ToString()), max, Encoding.Default.GetByteCount(textboxCell.Value?.ToString()) - max);
            //    //            textboxCell.Value = text1;
            //    //            dataGridView1.Rows.Insert(e.RowIndex + 1, new object[] { text2, dataGridView1.Rows[e.RowIndex].Cells["停留时间"].Value.ToString(), dataGridView1.Rows[e.RowIndex].Cells["字号"].Value.ToString(), dataGridView1.Rows[e.RowIndex].Cells["字体颜色"].Value.ToString() });

            //    //        }
            //    //        else
            //    //        {
            //    //            string text1 = Encoding.Default.GetString(Encoding.Default.GetBytes(textboxCell.Value?.ToString()), 0, max - 1);
            //    //            string text2 = Encoding.Default.GetString(Encoding.Default.GetBytes(textboxCell.Value?.ToString()), max - 1, Encoding.Default.GetByteCount(textboxCell.Value?.ToString()) - max+1);
            //    //            textboxCell.Value = text1;
            //    //            dataGridView1.Rows.Insert(e.RowIndex + 1, new object[] { text2, dataGridView1.Rows[e.RowIndex].Cells["停留时间"].Value.ToString(), dataGridView1.Rows[e.RowIndex].Cells["字号"].Value.ToString(), dataGridView1.Rows[e.RowIndex].Cells["字体颜色"].Value.ToString() });
            //    //        }
            //    //    }
            //    //}
            //    #endregion
            //}
        }

        private void dgvPlayList_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {

            AutoSplitScreen(e.RowIndex);
            //if (e.RowIndex > -1 && dgvPlayList.Rows[e.RowIndex].Cells["字号"].Value != null && dgvPlayList.Rows[e.RowIndex].Cells["显示内容"].Value != null && dgvPlayList.Rows[e.RowIndex].Cells["字号"].Value != null && dgvPlayList.Rows[e.RowIndex].Cells["字体颜色"].Value != null)
            //{
            //    DataGridViewTextBoxCell textboxCell = (DataGridViewTextBoxCell)dgvPlayList.Rows[e.RowIndex].Cells["显示内容"];
            //    DataGridViewComboBoxCell comboxCell = (DataGridViewComboBoxCell)dgvPlayList.Rows[e.RowIndex].Cells["字号"];

            //    int max = GetMaxSbcTextCountInScreen(Convert.ToInt32(comboxCell.Value.ToString()), 128, 64);
            //    string noEnterString = textboxCell.Value?.ToString().Replace(@"\n", string.Empty);
            //    //当字符数量超过一个屏幕的时候，对字符串进行切割
            //    if (textboxCell.Value != null && Encoding.Default.GetByteCount(noEnterString) > max)
            //    {
            //        string lastChar = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max).Substring(Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max).Length - 1, 1);
            //        //这段代码主要是为了判断有没有讲一个双字节字符切成了两个单字节字符，如果被切出现乱码，乱码会以'?'形式表现，以此来判断
            //        if (lastChar != "?")
            //        {
            //            string text1 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max);
            //            string text2 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), max, Encoding.Default.GetByteCount(noEnterString) - max);
            //            dgvPlayList.CellValueChanged -= dgvPlayList_CellValueChanged;
            //            //textboxCell.Value = insertEnterIntoString(text1, comboxCell.Value.ToString());//将本屏幕内剩余的字符串添加回车符
            //            textboxCell.Value = insertEnterIntoString2(text1, Convert.ToInt32(comboxCell.Value.ToString()), 128, 64);
            //            dgvPlayList.Rows.Insert(e.RowIndex + 1, new object[] { text2, dgvPlayList.Rows[e.RowIndex].Cells["停留时间"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["字号"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["字体颜色"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["坐标X"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["坐标Y"].Value.ToString() });
            //            dgvPlayList.CellValueChanged += dgvPlayList_CellValueChanged;
            //        }
            //        else //如果是奇数个，则需要往前提前半个全角字符截取
            //        {
            //            string text1 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), 0, max - 1);
            //            string text2 = Encoding.Default.GetString(Encoding.Default.GetBytes(noEnterString), max - 1, Encoding.Default.GetByteCount(noEnterString) - max + 1);
            //            dgvPlayList.CellValueChanged -= dgvPlayList_CellValueChanged;
            //            //textboxCell.Value = insertEnterIntoString(text1, comboxCell.Value.ToString());
            //            textboxCell.Value = insertEnterIntoString2(text1, Convert.ToInt32(comboxCell.Value.ToString()), 128, 64);
            //            dgvPlayList.Rows.Insert(e.RowIndex + 1, new object[] { text2, dgvPlayList.Rows[e.RowIndex].Cells["停留时间"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["字号"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["字体颜色"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["坐标X"].Value.ToString(), dgvPlayList.Rows[e.RowIndex].Cells["坐标Y"].Value.ToString() });
            //            dgvPlayList.CellValueChanged += dgvPlayList_CellValueChanged;
            //        }
            //    }
            //    //当字符数量小于一个屏幕的时候，给当前屏幕添加回车符
            //    if (Encoding.Default.GetByteCount(noEnterString) <= max)
            //    {
            //        //textboxCell.Value = insertEnterIntoString(noEnterString, comboxCell.Value.ToString());
            //        textboxCell.Value = insertEnterIntoString2(noEnterString, Convert.ToInt32(comboxCell.Value.ToString()), 128, 64);
            //    }
            //    DataGridViewTextBoxCell textboxCellX = (DataGridViewTextBoxCell)dgvPlayList.Rows[e.RowIndex].Cells["坐标X"];
            //    DataGridViewTextBoxCell textboxCellY = (DataGridViewTextBoxCell)dgvPlayList.Rows[e.RowIndex].Cells["坐标Y"];
            //    int fontsize = int.Parse(comboxCell.Value.ToString());
            //    refreshFCMS();//将新的字符串刷入预览LABEL，之后按照新的字符串计算位置
            //    textboxCellX.Value = (128 - ((fontsize / 2) * MaxLineLength(textboxCell.Value.ToString()))) / 2;
            //    textboxCellY.Value = (64 - (fontsize * GetLinesCount(textboxCell.Value.ToString()))) / 2;
            //    this.lableTextPrewiew.Location = new Point((int)((this.panelUp.Width - this.lableTextPrewiew.Width) / 2), (int)((this.panelUp.Height - this.lableTextPrewiew.Height) / 2));
            //    refreshFCMS();
            //}
        }
        /// <summary>
        /// 计算带有\n分割的字符串的行数，如果最后一行为空，则改行不计入总行数
        /// </summary>
        /// <param name="text">要被计算的文本</param>
        /// <returns></returns>
        private int GetLinesCount(string text)
        {
            string[] tempArray = Regex.Split(text, @"\\n");
            int count = tempArray.Length;
            if (tempArray[count - 1] == string.Empty)
            {
                count--;
            }
            return count;
        }
        /// <summary>
        /// 计算带有\n分割的字符串最大一行的字节数
        /// </summary>
        /// <param name="text">要被计算的文本</param>
        /// <returns></returns>
        private int MaxLineLength(string text)
        {
            string[] tempArray = Regex.Split(text, @"\\n"); ;
            int length = tempArray.Length;

            int maxLineLength = 0;
            foreach (var temp in tempArray)
            {
                if (GetLength(temp) >= maxLineLength)
                {
                    maxLineLength = GetLength(temp);
                }
            }
            return maxLineLength;
        }

        /// <summary>
        /// 获得某个字符串的字节长度
        /// </summary>
        /// <param name="str">用以被计算的字符串</param>
        /// <returns></returns>
        public static int GetLength(string str)
        {
            int length = 0;
            if (str.Length == 0)
            {
                return length;
            }
            byte[] tempArray = Encoding.ASCII.GetBytes(str);
            foreach (var temp in tempArray)
            {
                if (temp == 0x3f)
                {
                    length += 2;
                }
                else
                {
                    length++;
                }
            }
            return length;
        }
        /// <summary>
        /// 判断是否为全角字
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public bool IsDBC(char c)
        {
            if (c > 255)
            {
                return true;
            }
            return false;
        }


        private string InsertEnterIntoString(string text, int fontSize, int screenWidth, int screenHeight)
        {
            string enterString = @"\n";
            string textWithoutEnter = text.Replace("\r\n", string.Empty);
            bool needInsertEnterFlag = true;
            StringBuilder resultStringBuilder = new StringBuilder();

            while (needInsertEnterFlag)
            {
                needInsertEnterFlag = false;
                int count = 0;
                int index = 0;
                foreach (var textChar in textWithoutEnter)
                {
                    if (!IsDBC(textChar))
                    {
                        count += 1;
                        index += 1;
                    }
                    else
                    {
                        count += 2;
                        index += 1;
                    }
                    if (count <= ((screenWidth * 2) / fontSize) - 1)
                    {
                        resultStringBuilder.Append(textChar);
                        needInsertEnterFlag = false;
                    }
                    else
                    {
                        
                        if (count == (screenWidth * 2) / fontSize)
                        {
                            resultStringBuilder.Append(textChar);
                            resultStringBuilder.Append(enterString);
                            textWithoutEnter = textWithoutEnter.Substring(index);
                            needInsertEnterFlag = true;
                            break;
                        }
                        else
                        {
                            resultStringBuilder.Append(enterString);
                            textWithoutEnter = textWithoutEnter.Substring(index - 1);
                            needInsertEnterFlag = true;
                            break;
                        }
                    }
                }
            }            
            return resultStringBuilder.ToString();
            
        }

        /// <summary>
        /// （旧方法，已弃用）按照屏幕字体大小对字符串插入\n换行符
        /// </summary>
        /// <param name="text">文本内容</param>
        /// <param name="size">字体大小</param>
        /// <returns></returns>
        [Obsolete("此方法已更新为新的方法，InsertEnterIntoString", false)]
        private string insertEnterIntoString(string text, string size)
        {

            string enter = @"\n";
            string str1 = "";
            string str2 = "";
            string str3 = "";
            string str4 = "";
            string result = "";
            string result2 = "";
            string result3 = "";
            switch (size)
            {
                case "16": //16号字体，最多可以三行
                    {
                        string msg = text.Replace("\r\n", "");
                        int count = 0;
                        int index = 0;
                        foreach (char x in msg)
                        {

                            if (!IsDBC(x))
                            {
                                count = count + 1;
                                index = index + 1;
                            }
                            if (IsDBC(x))
                            {
                                count = count + 2;
                                index = index + 1;
                            }
                            if (count <= 14)
                            {
                                result = msg;
                            }
                            msg = msg.Replace(@"\n", "");
                            if (count == 15 || count == 16)
                            {

                                msg = msg.Insert(index, enter);
                                result = msg;
                                str1 = msg.Substring(0, index + 2);
                                str2 = msg.Substring(index + 2);
                            }
                        }
                        if (str2 != string.Empty)//如果第二行存在，则对第二行进行一下判断
                        {
                            int count2 = 0;
                            int index2 = 0;
                            foreach (char y in str2)
                            {
                                if (!IsDBC(y))
                                {
                                    count2 = count2 + 1;
                                    index2 = index2 + 1;
                                }
                                if (IsDBC(y))
                                {
                                    count2 = count2 + 2;
                                    index2 = index2 + 1;
                                }
                                if (count <= 14)
                                {
                                    result2 = str2;
                                }
                                str2 = str2.Replace(@"\n", "");
                                if (count2 == 15 || count2 == 16)
                                {
                                    str2 = str2.Insert(index2, enter);
                                    result = str1 + str2;
                                    str3 = str2.Substring(index2 + 2);
                                    result2 = str2.Substring(0, index2 + 2);


                                }
                            }
                        }
                        if (str3 != string.Empty)
                        {
                            int count3 = 0;
                            int index3 = 0;
                            foreach (char z in str3)
                            {
                                if (!IsDBC(z))
                                {
                                    count3 = count3 + 1;
                                    index3 = index3 + 1;
                                }
                                if (IsDBC(z))
                                {
                                    count3 = count3 + 2;
                                    index3 = index3 + 1;
                                }
                                if (count3 <= 14)
                                {
                                    result3 = str3;
                                }
                                str3 = str3.Replace(@"\n", "");
                                if (count3 == 15 || count3 == 16)
                                {
                                    str3 = str3.Insert(index3, enter);
                                    //str4 = str4.Substring(index3 + 2);
                                    result = str1 + result2 + str3;
                                }
                            }
                        }

                    }
                    break;
                case "24"://24号字体，最多两行
                    {
                        string msg = text.Replace("\r\n", "");
                        int count = 0;
                        int index = 0;
                        foreach (char x in msg)
                        {

                            if (!IsDBC(x))
                            {
                                count = count + 1;
                                index = index + 1;
                            }
                            if (IsDBC(x))
                            {
                                count = count + 2;
                                index = index + 1;
                            }
                            if (count <= 8)
                            {
                                result = msg;
                            }
                            msg = msg.Replace(@"\n", "");
                            if (count == 9 || count == 10)
                            {
                                msg = msg.Insert(index, enter);
                                result = msg;
                                str1 = msg.Substring(0, index + 2);
                                str2 = msg.Substring(index + 2);
                            }
                        }
                    }
                    break;
                case "32"://32号字体 最多两行
                    {
                        string msg = text.Replace("\r\n", "");
                        int count = 0;
                        int index = 0;
                        foreach (char x in msg)
                        {

                            if (!IsDBC(x))
                            {
                                count = count + 1;
                                index = index + 1;
                            }
                            if (IsDBC(x))
                            {
                                count = count + 2;
                                index = index + 1;
                            }
                            if (count <= 6)
                            {
                                result = msg;
                            }
                            msg = msg.Replace(@"\n", "");
                            if (count == 7 || count == 8)
                            {
                                msg = msg.Insert(index, enter);
                                result = msg;
                                str1 = msg.Substring(0, index + 2);
                                str2 = msg.Substring(index + 2);
                            }
                        }
                    }
                    break;
                case "48"://48号字体 最多一行
                    {
                        result = text;
                    }
                    break;
                case "64"://48号字体 最多一行
                    {
                        result = text;
                    }
                    break;
            }
            return result;

        }

        public void RefreshFCMS()
        {
            if (dgvPlayList.SelectedRows.Count != 0)
            {
                try
                {
                    int rowIndex = this.dgvPlayList.SelectedCells[0].RowIndex;
                    DisplayElement element = new DisplayElement();
                    element.Msg = this.dgvPlayList.Rows[rowIndex].Cells["显示内容"].Value.ToString();
                    element.MsgX = this.dgvPlayList.Rows[rowIndex].Cells["坐标X"].Value.ToString();
                    element.MsgY = this.dgvPlayList.Rows[rowIndex].Cells["坐标Y"].Value.ToString();
                    element.Delay = this.dgvPlayList.Rows[rowIndex].Cells["停留时间"].Value.ToString();
                    element.FontSize = this.dgvPlayList.Rows[rowIndex].Cells["字号"].Value.ToString();
                    element.Colour = this.dgvPlayList.Rows[rowIndex].Cells["字体颜色"].Value.ToString();

                    this.lableTextPrewiew.Location = new Point(0, 0);

                    switch (element.Colour)
                    {
                        case "红色":
                            this.lableTextPrewiew.ForeColor = Color.Red;
                            break;

                        case "绿色":
                            this.lableTextPrewiew.ForeColor = Color.LawnGreen;
                            break;

                        case "蓝色":
                            this.lableTextPrewiew.ForeColor = Color.DeepSkyBlue;
                            break;

                        case "黄色":
                            this.lableTextPrewiew.ForeColor = Color.Yellow;
                            break;
                    }



                    switch (element.FontSize)
                    {
                        case "16":
                            this.simFontSize = 0x10 * this.scrCoeff;
                            break;

                        case "24":
                            this.simFontSize = 0x18 * this.scrCoeff;
                            break;

                        case "32":
                            this.simFontSize = 0x20 * this.scrCoeff;
                            break;

                        case "48":
                            this.simFontSize = 0x30 * this.scrCoeff;
                            break;
                        case "64":
                            this.simFontSize = 0x40 * this.scrCoeff;
                            break;
                    }

                    this.lableTextPrewiew.Font = new Font(this.simFont, (float)this.simFontSize, GraphicsUnit.Pixel);
                    //this.lableTextPrewiew.Location = new Point(int.Parse(element.MsgX) * scrCoeff, int.Parse(element.MsgY) * scrCoeff);

                    int index = element.Msg.IndexOf(@"\n");
                    if (index == -1)
                    {
                        this.lableTextPrewiew.Text = element.Msg;
                    }
                    else
                    {
                        string msg = element.Msg;

                        lableTextPrewiew.Text = msg.Replace(@"\n", "\r\n");
                    }
                    this.lableTextPrewiew.Location = new Point((int)((this.panelUp.Width - this.lableTextPrewiew.Width) / 2), (int)((this.panelUp.Height - this.lableTextPrewiew.Height) / 2));
                }
                catch { }

            }

        }
        private void dgvPlayList_SelectionChanged(object sender, EventArgs e)
        {
            RefreshFCMS();
        }



        /// <summary>
        /// 填写情报板下拉列表
        /// </summary>
        public void getFcmss()
        {
            SqlDBHelper sdh = new SqlDBHelper();
            string sqlGetFcmss = "SELECT M_Device.DeviceID+'$'+R_FcmsStatus.ParamName+'$'+R_FcmsStatus.PlayListID+'$'+R_FcmsStatus.ControlSwitch+'$'+M_Device.Protocol as IDandPARAM, M_Device.Name, R_FcmsStatus.ParamName FROM M_Device LEFT OUTER JOIN R_FcmsStatus ON M_Device.DeviceID = R_FcmsStatus.FCMSID WHERE (M_Device.TypeID = 'FCMS')";
            DataTable dtGetFcmss = sdh.GetTable(sqlGetFcmss);
            cbFcmss.DataSource = dtGetFcmss;
        }

        DataTable dtGetPlayList = new DataTable();
        /// <summary>
        /// 填写播放列表下拉列表
        /// </summary>
        public void getPlayList()
        {
            SqlDBHelper sdh = new SqlDBHelper();
            string sqlGetPlayList = "SELECT [PlayListID],[Name],[UsingFCMS] FROM [TunnelXAT1].[dbo].[M_PlayList]";
            dtGetPlayList = sdh.GetTable(sqlGetPlayList);
            cbPlayList.DataSource = dtGetPlayList;
        }
        private void FCMSForm_Load(object sender, EventArgs e)
        {
            //getFcmss();

            //getPlayList();

            //fillTheGridView();

            //cbFcmss_SelectedValueChanged(null, null);
        }

        private void addList_Click(object sender, EventArgs e)
        {
            FCMSPlayListAdd f = new FCMSPlayListAdd(this);
            f.StartPosition = FormStartPosition.CenterScreen;
            f.ShowDialog();
        }



        private void deleteList_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定删除这个播放列表及其全部内容吗？", "请确认", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                string sqlDeletePlayList = "DELETE FROM M_PlayList WHERE PlayListID='" + cbPlayList.SelectedValue.ToString() + "'";
                string sqlDeletePlayListInfo = "DELETE FROM M_PlayListInfo WHERE PlayListID='" + cbPlayList.SelectedValue.ToString() + "'";
                string[] sqlDeleteList = { sqlDeletePlayList, sqlDeletePlayListInfo };
                SqlDBHelper sdh = new SqlDBHelper();
                int count = sdh.ExecteTranCommand(sqlDeleteList);
                if (count != 0)
                {
                    MessageBox.Show("删除成功！");
                }
                getPlayList();

            }
        }

        private void rowUp_Click(object sender, EventArgs e)
        {
            int rowIndex = dgvPlayList.SelectedRows[0].Index;  //得到当前选中行的索引     

            if (rowIndex == 0)
            {
                MessageBox.Show("已经是第一行了!");
                return;
            }

            List<string> list = new List<string>();
            for (int i = 0; i < dgvPlayList.Columns.Count; i++)
            {
                if (dgvPlayList.SelectedRows[0].Cells[i].Value != null)
                {
                    list.Add(dgvPlayList.SelectedRows[0].Cells[i].Value.ToString());   //把当前选中行的数据存入list数组中    
                }
                else
                {
                    list.Add("");
                }
            }
            dgvPlayList.Rows[rowIndex].Selected = false;
            dgvPlayList.CellValueChanged -= dgvPlayList_CellValueChanged;
            for (int j = 0; j < dgvPlayList.Columns.Count; j++)
            {
                dgvPlayList.Rows[rowIndex].Cells[j].Value = dgvPlayList.Rows[rowIndex - 1].Cells[j].Value;
                dgvPlayList.Rows[rowIndex - 1].Cells[j].Value = list[j].ToString();
            }
            dgvPlayList.Rows[rowIndex - 1].Selected = true;
            dgvPlayList.CellValueChanged += dgvPlayList_CellValueChanged;

            RefreshFCMS();
        }

        private void rowDown_Click(object sender, EventArgs e)
        {
            int rowIndex = dgvPlayList.SelectedRows[0].Index;  //得到当前选中行的索引     

            if (rowIndex == dgvPlayList.Rows.Count - 1)
            {
                MessageBox.Show("已经是最后一行了!");
                return;
            }

            List<string> list = new List<string>();
            for (int i = 0; i < dgvPlayList.Columns.Count; i++)
            {
                if (dgvPlayList.SelectedRows[0].Cells[i].Value != null)
                {
                    list.Add(dgvPlayList.SelectedRows[0].Cells[i].Value.ToString());   //把当前选中行的数据存入list数组中    
                }
                else
                {
                    list.Add("");
                }
            }
            dgvPlayList.Rows[rowIndex].Selected = false;
            dgvPlayList.CellValueChanged -= dgvPlayList_CellValueChanged;
            for (int j = 0; j < dgvPlayList.Columns.Count; j++)
            {
                dgvPlayList.Rows[rowIndex].Cells[j].Value = dgvPlayList.Rows[rowIndex + 1].Cells[j].Value;
                dgvPlayList.Rows[rowIndex + 1].Cells[j].Value = list[j].ToString();
            }
            dgvPlayList.Rows[rowIndex + 1].Selected = true;
            dgvPlayList.CellValueChanged += dgvPlayList_CellValueChanged;
            RefreshFCMS();
        }

        private void rowInsert_Click(object sender, EventArgs e)
        {
            dgvPlayList.Rows.Add(new object[] { "", "1", "24", "红色", "0", "0" });
            dgvPlayList.Rows[dgvPlayList.Rows.Count - 1].Selected = true;

        }

        private void rowDelete_Click(object sender, EventArgs e)
        {
            dgvPlayList.Rows.Remove(dgvPlayList.SelectedRows[0]);
        }

        private void listSave_Click(object sender, EventArgs e)
        {
            int number = 1;
            List<string> lslist = new List<string>();
            string sqlClear = "delete from M_PlayListInfo where PlayListID='" + playListId + "'";
            lslist.Add(sqlClear);
            foreach (DataGridViewRow dgvr in dgvPlayList.Rows)
            {
                if (!string.IsNullOrEmpty(dgvr.Cells["显示内容"].Value.ToString()))
                {
                    Global g = new Global();
                    string sqlInfoInsert = "INSERT INTO M_PlayListInfo(PlayID,PlayListID,Text,StayTime,FontSize,FontColor,TextX,TextY,Number) VALUES ('" + g.CommandID + "','" + playListId + "','" + dgvr.Cells["显示内容"].Value.ToString() + "','" + dgvr.Cells["停留时间"].Value.ToString() + "','" + dgvr.Cells["字号"].Value.ToString() + "','" + dgvr.Cells["字体颜色"].Value.ToString() + "','" + dgvr.Cells["坐标X"].Value.ToString() + "','" + dgvr.Cells["坐标Y"].Value.ToString() + "','" + number + "')";
                    number++;
                    lslist.Add(sqlInfoInsert);
                }
            }
            string[] sqlListSave = lslist.ToArray();
            SqlDBHelper sdh = new SqlDBHelper();
            int i = sdh.ExecteTranCommand(sqlListSave);
            if (i != 0)
            {
                MessageBox.Show("已保存该播放列表，共" + (number - 1).ToString() + "条", "保存成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void cbFcmss_SelectedValueChanged(object sender, EventArgs e)
        {
            string[] idAndParam = cbFcmss.SelectedValue.ToString().Split('$');
            fcmsId = idAndParam[0];
            selectFcmsParamName = idAndParam[1];
            fcmsPlaylistID = idAndParam[2];
            controlSwitch = idAndParam[3];
            string plc = idAndParam[4];
            if (fcmsPlaylistID != null)
            {
                cbPlayList.SelectedValue = fcmsPlaylistID;
            }
            if (!Global.PlcStatus[plc])
            {
                publishList.Enabled = false;
            }
            else
            {
                publishList.Enabled = true;
            }
        }

        private void cbPlayList_SelectedValueChanged(object sender, EventArgs e)
        {
            playListId = cbPlayList.SelectedValue.ToString();
            fillTheGridView();
        }
        /// <summary>
        /// 通过播放列表ID获取播放列表信息并写入GRIDVIEW
        /// </summary>
        private void fillTheGridView()
        {
            string sqlFillGridView = "SELECT Text, StayTime, FontSize, FontColor,TextX,TextY,Number FROM M_PlayListInfo WHERE PlayListID = '" + playListId + "' ORDER BY Number";
            SqlDBHelper sdh = new SqlDBHelper();
            dtPlayListInfo = sdh.GetTable(sqlFillGridView);

            dgvPlayList.Rows.Clear();

            dgvPlayList.CellValueChanged -= dgvPlayList_CellValueChanged;
            foreach (DataRow dr in dtPlayListInfo.Rows)
            {
                dgvPlayList.Rows.Add(1);
                DataGridViewRow lastDr = dgvPlayList.Rows[dgvPlayList.RowCount - 1];
                lastDr.Cells["显示内容"].Value = dr["Text"].ToString();
                lastDr.Cells["停留时间"].Value = dr["StayTime"].ToString();
                lastDr.Cells["字号"].Value = dr["FontSize"].ToString();
                lastDr.Cells["字体颜色"].Value = dr["FontColor"].ToString();
                lastDr.Cells["坐标X"].Value = dr["TextX"].ToString();
                lastDr.Cells["坐标Y"].Value = dr["TextY"].ToString();
            }
            dgvPlayList.CellValueChanged += dgvPlayList_CellValueChanged;
        }



        private void 细节调整ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvPlayList.SelectedRows.Count == 1)
            {
                int rowIndex = this.dgvPlayList.SelectedCells[0].RowIndex;
                DisplayElement element = new DisplayElement();
                element.Msg = this.dgvPlayList.Rows[rowIndex].Cells["显示内容"].Value.ToString();
                element.MsgX = this.dgvPlayList.Rows[rowIndex].Cells["坐标X"].Value.ToString();
                element.MsgY = this.dgvPlayList.Rows[rowIndex].Cells["坐标Y"].Value.ToString();
                element.Delay = this.dgvPlayList.Rows[rowIndex].Cells["停留时间"].Value.ToString();
                element.FontSize = this.dgvPlayList.Rows[rowIndex].Cells["字号"].Value.ToString();
                element.Colour = this.dgvPlayList.Rows[rowIndex].Cells["字体颜色"].Value.ToString();
                using (FcmsPreviewForm preview = new FcmsPreviewForm(element))
                {
                    preview.fontSizecomboBox.Text = element.FontSize;
                    preview.colorcomboBox.Text = element.Colour;
                    preview.showTypecomboBox.Text = "立即出现";
                    preview.fontcomboBox.Text = "宋体";

                    if (preview.ShowDialog() == DialogResult.OK)
                    {
                        dgvPlayList.CellValueChanged -= dgvPlayList_CellValueChanged;
                        this.dgvPlayList.CurrentCell = null;
                        this.dgvPlayList.Rows[rowIndex].Cells["显示内容"].Value = element.Msg;
                        this.dgvPlayList.Rows[rowIndex].Cells["坐标X"].Value = element.MsgX;
                        this.dgvPlayList.Rows[rowIndex].Cells["坐标Y"].Value = element.MsgY;
                        //this.dgvPlayList.Rows[rowIndex].Cells[8].Value = element.Font;
                        this.dgvPlayList.Rows[rowIndex].Cells["字号"].Value = element.FontSize;
                        this.dgvPlayList.Rows[rowIndex].Cells["字体颜色"].Value = element.Colour;
                        //this.dgvPlayList.Rows[rowIndex].Cells[11].Value = element.ShowType;
                        dgvPlayList.CellValueChanged += dgvPlayList_CellValueChanged;                        
                    }
                    return;
                }
            }
        }

        private void dgvPlayList_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    //若行已是选中状态就不再进行设置
                    if (dgvPlayList.Rows[e.RowIndex].Selected == false)
                    {
                        dgvPlayList.ClearSelection();
                        dgvPlayList.Rows[e.RowIndex].Selected = true;
                    }
                    //只选中一行时设置活动单元格
                    //if (dgvPlayList.SelectedRows.Count == 1)
                    //{
                    //    dgvPlayList.CurrentCell = dgvPlayList.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    //}
                    //弹出操作菜单
                    cmsDGV.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }
        List<string> savelog = new List<string>();
        private void publishList_Click(object sender, EventArgs e)
        {
            try
            {
                savelog.Clear();
                string info = "";
                if (dgvPlayList.RowCount == 0)
                {
                    MessageBox.Show("播放列表为空，请先添加情报板内容", "播放列表为空", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    Getplaylist getplaylist = new Getplaylist();
                    List<byte> file = new List<byte>();
                    getplaylist.ClearItem();
                    foreach (DataGridViewRow dgvr in dgvPlayList.Rows)
                    {
                        DisplayElement element = new DisplayElement();
                        element.Msg = dgvr.Cells["显示内容"].Value.ToString();
                        element.MsgX = dgvr.Cells["坐标X"].Value.ToString();
                        element.MsgY = dgvr.Cells["坐标Y"].Value.ToString();
                        element.Delay = (int.Parse(dgvr.Cells["停留时间"].Value.ToString()) * 100).ToString();
                        element.FontSize = dgvr.Cells["字号"].Value.ToString() + dgvr.Cells["字号"].Value.ToString();
                        element.Colour = dgvr.Cells["字体颜色"].Value.ToString();
                        savelog.Add(element.ToString());
                        getplaylist.AddItem(element);
                    }
                    file = getplaylist.GetPlaylst();
                    using (StreamWriter writer = new StreamWriter(Application.StartupPath + @"\\play.list", false, Encoding.Default))
                    {
                        writer.Write(Encoding.Default.GetString(file.ToArray()));
                        //JX_msg.JX_UploadFile("play.lst", ref file, "");
                        bool successed = JX_msg.JX_UploadFile("play.lst", ref file, fcmsId, selectFcmsParamName, controlSwitch, this);

                    }
                }

            }
            catch { }
        }
        public void changeFcmsList()
        {
            SqlDBHelper sdh = new SqlDBHelper();
            string sql = "UPDATE [R_FcmsStatus] SET [PlayListID]='" + playListId + "' WHERE [FCMSID]='" + fcmsId + "'";
            string fcmstext = "";
            foreach (var item in savelog)
            {
                fcmstext = fcmstext + " " + item;
            }
            string sql2 = "insert into H_FCMSLog(FLogID,FCMSID,FCMSTEXT,UserID,UpdateTime) VALUES ('" + new Global().CommandID + "','" + fcmsId + "','" + fcmstext + "','" + Global.UserID + "','" + DateTime.Now.ToString() + "')";
            sdh.ExecteTranCommand(new string[] { sql, sql2 });
            MessageBox.Show("指令下发成功");
        }

        private void preInstall_Click(object sender, EventArgs e)
        {
            try
            {
                string info = "";
                if (dgvPlayList.RowCount == 0)
                {
                    MessageBox.Show("播放列表为空，请先添加情报板内容", "播放列表为空", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    Getplaylist getplaylist = new Getplaylist();
                    List<byte> file = new List<byte>();
                    getplaylist.ClearItem();
                    foreach (DataGridViewRow dgvr in dgvPlayList.Rows)
                    {
                        DisplayElement element = new DisplayElement();
                        element.Msg = dgvr.Cells["显示内容"].Value.ToString();
                        element.MsgX = dgvr.Cells["坐标X"].Value.ToString();
                        element.MsgY = dgvr.Cells["坐标Y"].Value.ToString();
                        element.Delay = (int.Parse(dgvr.Cells["停留时间"].Value.ToString()) * 100).ToString();
                        element.FontSize = dgvr.Cells["字号"].Value.ToString() + dgvr.Cells["字号"].Value.ToString();
                        element.Colour = dgvr.Cells["字体颜色"].Value.ToString();

                        getplaylist.AddItem(element);
                    }
                    file = getplaylist.GetPlaylst();
                    using (StreamWriter writer = new StreamWriter(Application.StartupPath + @"\\play" + cbInstallNo.Text + ".list", false, Encoding.Default))
                    {
                        writer.Write(Encoding.Default.GetString(file.ToArray()));
                        //JX_msg.JX_UploadFile("play.lst", ref file, "");
                        bool successed = JX_msg.JX_UploadFile("PLAYLIST\\" + cbInstallNo.Text + ".list", ref file, fcmsId, selectFcmsParamName, controlSwitch, this);

                    }
                }

            }
            catch { }
        }
    }
}
